package top.shiyiri.team.view;

import top.shiyiri.team.domain.Employee;
import top.shiyiri.team.domain.Programmer;
import top.shiyiri.team.service.NameListService;
import top.shiyiri.team.service.TeamException;
import top.shiyiri.team.service.TeamService;

public class TeamView {

    private NameListService listSvc = new NameListService();
    private TeamService teamSvc = new TeamService();

    public void enterMainMenu() {
        boolean loopFlag = true;
        char menu = '0';
        while (loopFlag) {
            if (menu != '1') {
                listAllEmployees();
            }
            System.out.print("1-团队列表  2-添加团队成员  3-删除团队成员 4-退出   请选择(1-4)：\n");
            menu = TSUtility.readMenuSelection();
            switch (menu) {
                case '1':
                    getTeam();
                    break;
                case '2':
                    addMember();
                    break;
                case '3':
                    deleteMember();
                    break;
                case '4':
                    System.out.println("确认是否退出(Y/N)：");
                    char c1 = TSUtility.readConfirmSelection();
                    if (c1 == 'Y' || c1 == 'N') {
                        loopFlag = false;
                    }
                    break;
            }
        }
    }

    /**
     * @Description 显示所有的员工信息
     */
    private void listAllEmployees() {
        System.out.println("-------------------------------------开发团队调度软件--------------------------------------\n");
        Employee[] employees = listSvc.getEmployees();

        if (employees == null || employees.length == 0) {
            System.out.println("公司暂无信息");
        } else {
            System.out.println("ID\t姓名\t年龄\t工资\t职位\t状态\t奖金\t\t股票\t领用设备");

            for (Employee employee : employees) {
                System.out.println(employee);
            }
        }

        System.out.println("----------------------------------------------------------------------------");

    }

    private void getTeam() {
        System.out.println("--------------------------------------团队成员列表--------------------------------------");
        Programmer[] team = teamSvc.getTeam();
        if (team == null || team.length == 0) {
            System.out.println("开发团队没有人");
        } else {
            System.out.println("TID/ID\t姓名\t年龄\t工资\t职位\t奖金\t股票");
            for (int i = 0; i < team.length; i++) {
                System.out.println(team[i].getDetails2());
            }
        }
        System.out.println("----------------------------------------------------------------------------");

    }

    private void addMember() {
        System.out.println("--------------------------------------添加成员--------------------------------------\n");

        System.out.print("请输入要添加的员工ID：");
        int id = TSUtility.readInt();
        try {
            Employee employee = listSvc.getEmployee(id);
            teamSvc.addMember(employee);
            System.out.println("添加成功");
        } catch (TeamException e) {
            System.out.println("添加失败，原因：" + e.getMessage());
        }
        TSUtility.readReturn();
    }

    private void deleteMember() {
        System.out.println("--------------------------------------删除成员--------------------------------------\n");
        System.out.print("请输入要删除的员工TID：");
        int memberId = TSUtility.readInt();

        System.out.println("确认是否删除(Y/N)：");
        char isDelete = TSUtility.readConfirmSelection();
        if (isDelete == 'N') {
            return;
        }
        try {
            teamSvc.removeMember(memberId);
            System.out.println("删除成功");
        } catch (TeamException e) {
            System.out.println("删除失败，原因：" + e.getMessage());
        }
        TSUtility.readReturn();
    }

    public static void main(String[] args) {
        TeamView view = new TeamView();
        view.enterMainMenu();
    }

}
